//
//  NetClient.m
//  Lesson
//
//  Created by centling on 15/4/19.
//  Copyright (c) 2015年 Seas.CHENG. All rights reserved.
//

#import "NetClient.h"
#import "global.h"

@implementation NetClient

+ (NetClient *) instance {
    static NetClient *netClient;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        netClient=[[NetClient alloc] init];
    });
    return netClient;
}


-(id)init{
    return [super initWithBaseURL:[NSURL URLWithString:@"http://api.map.baidu.com/telematics/v3"]];
}


-(void)getWeather:(NSString *)city completionBlock:(id(^)(Weather *weather, NSError *error))block{
    
    
//    NSString *plistPath = [[NSBundle mainBundle] pathForResource:@"Info"
//                                                          ofType:@"plist"];
//    NSDictionary *dictionary = [[NSDictionary alloc] initWithContentsOfFile:plistPath];
//    NSString *bundleIdentifier=[dictionary objectForKey:@"CFBundleIdentifier"];
    NSString *bundleIdentifier = [[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleIdentifier"];

    
    //url的UTF8编码
    NSString *url=[NSString stringWithFormat:@"weather?location=%@&output=json&ak=%@&mcode=%@",city,baiduAK,bundleIdentifier];
    
    NSString *encodedUrl = [url stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    [self GET:encodedUrl parameters:nil success:^(NSURLSessionDataTask *task, id responseObject) {
//        NSString *status=[(NSDictionary *)responseObject objectForKey:@"status"];
        Weather *weather=[MTLJSONAdapter modelOfClass:[Weather class] fromJSONDictionary:(NSDictionary *)responseObject error:nil];
        
        block(weather,nil);
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        block(nil,error);
    }];

}


@end
